<?php
defined('IN_MAIN') or exit('Invalid Access!');

function print_bonus_summary($in_year, $in_month, $in_uid=''){
/*
This function will format the bonus summary for the input in_date
*/

	global $vars;
	$db = $vars['db'];
	$r_type_d=array('s'=>"Sponsor Bonus",'p'=>"Pairing Bonus",'m'=>"Matching Bonus",'l'=>"LEadership Bonus");
	$country_code=$vars['common']['country_code'];
	array_shift($country_code);
	$country_code_t=$vars['common']['country_code_text'];
	foreach($country_code_t as $code=>$cname){
		$country_code_d[$code]=__($cname);
	}
	
	$month = $in_month;
	$year = $in_year;
	$comm_date=$year*12+$month;
	$calc_date['start']=date('Y-m-d H:i:s', mktime(0,0,0,$month,1,$year));
	$calc_date['end']=date('Y-m-d H:i:s', mktime(23,59,59,$month+1,0,$year));
	$calc_date_3month['start']=date('Y-m-d H:i:s', mktime(0,0,0,$month-2,1,$year));
	$uid_q = $in_uid? "uid in ($in_uid)" : "1=1";
	if(@mysql_num_rows(mysql_query($s="select * from $db->member_comm_sum where year='$year' and month='$month'"))){
		$use_summary = true;
	}else{
		$use_summary = false;
	}
	if(@mysql_num_rows(mysql_query($s="select * from $db->purchase_fig where year='$year' and month='$month'"))){
		$use_pspgs = true;
	}else{
		$use_pspgs = false;
	}
	if($use_summary){
		$t=@mysql_num_rows($r=mysql_query($s="select c.*, u.name, u.bank_name, u.bank_acc_no, u.country from $db->member_comm_sum c, $db->users u where c.uid=u.id and c.year*12+c.month=$comm_date and $uid_q order by c.uid"));
	}else{
		$t=@mysql_num_rows($r=mysql_query($s="select distinct(c.uid), u.name, u.bank_name, u.bank_acc_no, u.country from $db->member_comm c, $db->users u where c.uid=u.id and c.status='confirmed' and c.year*12+c.month=$comm_date and $uid_q order by c.uid"));
	}
	$k=0;
	for($i=0;$i<$t;$i++){
		$comm=mysql_fetch_assoc($r);
		if($use_pspgs){
			$group_sales_network = @mysql_result(mysql_query("select pgs_bv from $db->purchase_fig where uid='$comm[uid]' and year='$year' and month='$month'"), 0) or 0;
		}else{
			$personal_sales_network=@mysql_result(mysql_query("select sum(total_bv) from $db->purchase where uid='$comm[uid]' and first_sale='y' and no_bv!='y' and status='confirmed' and collected_date>='$calc_date[start]' and collected_date<='$calc_date[end]'"), 0) or 0;
			$group_sales_network=calc_group_sales($comm['uid'], $calc_date['start'], $calc_date['end'], 'matrix_upline') + $personal_sales_network;
		}
		$r_user = get_user_detail_by_id($comm['uid']);
		if($use_summary){
			$r_comm = $comm['cs'];
			$p_comm = $comm['cp'];
			$l_comm = $comm['cl'];
			$m_comm = $comm['cm'];
		}else{
			$r_comm=@mysql_result(mysql_query("select sum(amount) from $db->member_comm where uid='$comm[uid]' and type='s' and status='confirmed' and year*12+month=$comm_date"), 0) or 0;
			$p_comm=@mysql_result(mysql_query("select sum(amount) from $db->member_comm where uid='$comm[uid]' and type='p' and status='confirmed' and year*12+month=$comm_date"), 0) or 0;
			$m_comm=@mysql_result(mysql_query("select sum(amount) from $db->member_comm where uid='$comm[uid]' and type='m' and status='confirmed' and year*12+month=$comm_date"), 0) or 0;
			$l_comm=@mysql_result(mysql_query("select sum(amount) from $db->member_comm where uid='$comm[uid]' and type='l' and status='confirmed' and year*12+month=$comm_date"), 0) or 0;
		}
		$total=$r_comm+$p_comm+$l_comm+$m_comm;
		
		$bonus_detail="
		<table class='bonus-detail'>
			<tr>
				<th width='200' align='left'>Commission</th>
				<th align='right'>Amount (MYR)</th>
			</tr>
			<tr>
				<td>{$r_type_d['r']}:</td>
				<td align='right'>".number_format($r_comm, 2)."</td>
			</tr>
			<tr>
				<td>{$r_type_d['p']}:</td>
				<td align='right'>".number_format($p_comm, 2)."</td>
			</tr>
			<tr>
				<td>{$r_type_d['m']}:</td>
				<td align='right'>".number_format($m_comm, 2)."</td>
			</tr>
			<tr>
				<td>{$r_type_d['1']}:</td>
				<td align='right'>".number_format($p1_comm, 2)."</td>
			</tr>
			<tr>
				<td>{$r_type_d['2']}:</td>
				<td align='right'>".number_format($p2_comm, 2)."</td>
			</tr>
			<tr>
				<td>{$r_type_d['3']}:</td>
				<td align='right'>".number_format($p3_comm, 2)."</td>
			</tr>
			<tr>
				<td>{$r_type_d['u']}:</td>
				<td align='right'>".number_format($u_comm, 2)."</td>
			</tr>".($r_user['is_stockist'] == 'y'? "
			<tr>
				<td>{$r_type_d['t']}:</td>
				<td align='right'>".number_format($t_comm, 2)."</td>
			</tr>" : '')."
			<tr>
				<th class='bold' align='left'>Total Bonus:</th>
				<th align='right' class='bold'>".number_format($total, 2)."</th>
			</tr>
		</table>";
		
		//total sales
		if($use_pspgs){
			$r2 = mysql_query("select * from $db->purchase_fig where uid='$comm[uid]' and year='$year' and month='$month'");
			$sale_sum = mysql_fetch_assoc($r2);
			$personal_sales['bv'] = $sale_sum['ps_bv'];
			$personal_sales['dp'] = $sale_sum['ps_dp'];
			$personal_sales['rp'] = $sale_sum['ps_rp'];
			$group_sales['bv'] = $sale_sum['pgs_bv'];
			$group_sales['dp'] = $sale_sum['pgs_bv'];
			$group_sales['rp'] = $sale_sum['pgs_bv'];
		}else{
			$personal_sales = calc_personal_sales_print($comm['uid'], $calc_date['start'], $calc_date['end']);
			$group_sales = calc_group_sales_print($comm['uid'], $calc_date['start'], $calc_date['end'], 'matrix_upline');
			$group_sales['bv'] += $personal_sales['bv'];
			$group_sales['dp'] += $personal_sales['dp'];
			$group_sales['rp'] += $personal_sales['rp'];
		}
		
		//ranking
		$current_rank = $vars['rank_index_a_d'][$r_user['rank_a']];

		$bonus_summary .= ($bonus_summary? "\n<div class='page-break'></div>" : '')."
		<table class='print-header'>
			<tr>
				<td width='250'><img src='".M_URL."/images/site/logo.jpg' /></td>
				<td>Guang Fang International Marketing (M) Sdn. Bhd<br />
				1st Floor, No. 6, Jalan Salung 33/26<br />
				Shah Alam Technology Park<br />
				Section 33, 40400 Shah Alam<br />
				Selangor Darul Ehsan, Malaysia.<br />
				Tel: +603 51214278 / 6 Fax:	+603 51214197
				</td>
			</tr>
		</table>
		<table class='user-detail'>
			<tr>
				<td width='200' align='left'>Name:</td>
				<td>$r_user[name]</td>
			</tr>
			<tr>
				<td>Member ID:</td>
				<td>$r_user[id]</td>
			</tr>
			<tr>
				<td width='100'>Bonus Period:</td>
				<td>".date('F Y', mktime(0,0,0,$in_month, 1, $in_year))."</td>
			</tr>
		</table>
		$bonus_detail
		<p style='font-style:italic;'>This is a computer-generated document, no signature is required.</p>";
	}

	$bonus_summary = "<body style='margin:0px; padding:0px;' onload='window.print();'>$bonus_summary</body></html>";
	
	$print_css = "
	<style type='text/css'>
	div.page-break{ 
		page-break-before: always;
		border:0px solid #000; 
		height:1px;
		line-height:1px;
	}
	.bold{
		font-weight:bold;
	}
	p{
		font-family:arial;
		font-size:12px;
	}
	td, th{
		font-family:arial;
		font-size:12px;
		vertical-align:top;
	}
	table.user-detail, table.print-header, table.bonus-detail{
		width:100%;
		border-collapse:collapse;
		margin-bottom:10px;
	}
	table.user-detail td{
		padding:4px;
	}
	
	table.bonus-detail td, table.bonus-detail th{
		border:1px solid #aaa;
		font-size:12px;
		padding:4px;
	}
	table.bonus-detail th{
		background:#bbb;
	}
	table.in-detail{
		width:100%;
		border-collapse:collapse;
	}
	table.in-detail td{
		padding:0px;
		border:0px;
	}
	@page {
		.print-table {
			size:10cm 8cm;
			margin:0cm;
		}
	}
	</style>";
	
	$header = "
	<html>
	<head>
	<title>$vars[title] - Bonus Summary</title>
	$print_css
	</head>";
	
	return array('bonus_summary'=>$bonus_summary, 'header'=>$header);
}
?>